Implement parallel processing for log file reading and metrics aggregation#14
Implement parallel processing for log file reading and metrics aggregation#14RurikV wants to merge 5 commits intoClojure-Developer:mainfrom
Conversation
…ter confirming it's working properly, proceed to a multi-threaded approach. Add access log test data and implement unit tests for solution metrics - Created a new access log test file with extensive log entries for testing. - Implemented a Clojure test suite to validate the functionality of the solution. - Tests include calculations for total bytes, filtering by URL and referrer, and handling non-existent entries.
…dling Fetches Pokemon data asynchronously Implements asynchronous fetching of Pokémon types and details to improve performance. Adds error handling to gracefully manage potential API failures during data retrieval.
otus-16/src/otus_16/homework.clj
Outdated
| (def ^:const log-dir "./logs") | ||
|
|
||
| ;; Create a fixed thread pool for parallel processing | ||
| (def thread-pool (Executors/newFixedThreadPool |
There was a problem hiding this comment.
Если ниже тут же используешь (.addShutdownHook задефайненный объект внутри замыкания (.shutdown thread-pool) и т.п. , прямо по месту, то лучше использовать defonce. Иначе при перезугрузке неймспейса в репл или при подключении в качестве зависимости в несколько других неймспейсов, весь код неймспейса может вычислиться дважды, что приведет к передефайну thread-pool, а старый может остаться висеть в системе, потому что на него висит его шатдаун хук.
otus-16/src/otus_16/homework.clj
Outdated
| (+ 14 (.. Runtime getRuntime availableProcessors)))) | ||
|
|
||
| ;; Add a shutdown hook to ensure the thread pool is properly shut down | ||
| (.addShutdownHook (Runtime/getRuntime) |
There was a problem hiding this comment.
Если операция идемпотентная (следующие вызовы после первого ничего не изменяют), то ок. Иначе - при перезагрузке модуля может повеситься 2 хука, 2 листенера и т.п. Поэтому лучше такой прямой мутирующий код в теле неймспейса не писать, а выносить в функции типа init, которые вызывать единожды в точке входа (мэйн или его аналоги)
This prevents re-initialization of the thread pool if the code is reloaded, ensuring resource consistency and avoiding potential issues with duplicate thread pools. It is particularly useful in development environments with hot-reloading.
Moved shutdown logic into a dedicated init-thread-pool function for clarity and reusability. This improves lifecycle control and avoids side effects at the namespace level. Note: This function should be called explicitly once at application startup (e.g. -main) to avoid multiple shutdown hooks in case of module reload.
Add asynchronous fetching of Pokémon types and details with error handling
Fetches Pokemon data asynchronously
Implements asynchronous fetching of Pokémon types and details to improve performance.
Adds error handling to gracefully manage potential API failures during data retrieval.